import os
import io

class TestCase(object):
    pass

def calculateMatches(testCase):
    firstCandidates = set(testCase.cardgrid1[testCase.row1 - 1])
    secondCandidates = set(testCase.cardgrid2[testCase.row2 - 1])
    return list(firstCandidates.intersection(secondCandidates))
    pass

def parseInput(fileIn):
    numCases = int(fileIn.readline())
    testCases = [TestCase() for _ in range(numCases)]

    for i, testCase in enumerate(testCases):
        testCase.row1 = int(fileIn.readline())
        testCase.cardgrid1 = []
        for _ in range(4):
            rowStr = fileIn.readline()
            rowCards = str.split(rowStr)
            testCase.cardgrid1.append([int(card) for card in rowCards])
        testCase.row2 = int(fileIn.readline())
        testCase.cardgrid2 = []
        for _ in range(4):
            rowStr = fileIn.readline()
            rowCards = str.split(rowStr)
            testCase.cardgrid2.append([int(card) for card in rowCards])

    return testCases

def write_output(fileOut, caseNum, cards):
    cardStr = "Volunteer cheated!"
   
    if len(cards) == 1:
        cardStr = str(cards[0])
    elif len(cards) > 1:
        cardStr = "Bad magician!"  
    fileOut.write(str.format("Case #{0}: {1}\n", caseNum, cardStr))
    pass

if __name__ == "__main__":
    fileIn = open("CodeJamSolutions\\Input\\magictrick.txt")
    fileOut = open("CodeJamSolutions\\Output\\magictrick.txt", "w")
    testCases = parseInput(fileIn)
    for i, testCase in enumerate(testCases):
        matches = calculateMatches(testCase)
        write_output(fileOut, i+1, matches)
    fileIn.close()
    fileOut.close()

#Recently you went to a magic show. You were very impressed by one of the 
#tricks, so you decided to try to figure out the secret behind it!

#The magician starts by arranging 16 cards in a square grid: 4 rows of 
#cards, with 4 cards in each row. Each card has a different number from 
#1 to 16 written on the side that is showing. Next, the magician asks a 
#volunteer to choose a card, and to tell him which row that card is in.

#Finally, the magician arranges the 16 cards in a square grid again, 
#possibly in a different order. Once again, he asks the volunteer which 
#row her card is in. With only the answers to these two questions, the 
#magician then correctly determines which card the volunteer chose. 
#Amazing, right?

#You decide to write a program to help you understand the magician's 
#technique. The program will be given the two arrangements of the 
#cards, and the volunteer's answers to the two questions: the row 
#number of the selected card in the first arrangement, and the row 
#number of the selected card in the second arrangement. The rows are 
#numbered 1 to 4 from top to bottom.

#Your program should determine which card the volunteer chose; or 
#if there is more than one card the volunteer might have chosen 
#(the magician did a bad job); or if there's no card consistent with 
#the volunteer's answers (the volunteer cheated).